package com.alibaba.cirtus.ots.application.adapter.ability.strategy;

import com.alibaba.cz.base.tool.StringUtil;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsapplicationadaptersdk.contants.OtsApplicationAdapterSdkConstants;
import com.epoch.app.otsapplicationadaptersdk.interfacedistributestrategy.dto.WeightDistributionRequest;
import com.epoch.app.otsapplicationadaptersdk.interfacedistributestrategy.service.InterfaceDistributeStrategyService;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2022/6/29 21:12
 */
@Component
public class InterfaceDistributeStrategyImpl implements InterfaceDistributeStrategyService {
    private final Log log = Log.getLogger(InterfaceDistributeStrategyImpl.class);

    @Override
    public Result<String> weightDistribution(WeightDistributionRequest weightDistributionRequest) {
        String messageId = weightDistributionRequest.getMessageId();
        int i = 0;
        if(StringUtil.isNotEmpty(messageId)){
            i = Math.abs(messageId.hashCode()%10);
        }else{
            messageId = UUID.randomUUID().toString().replaceAll("-","");
            i = Math.abs(messageId.hashCode()%10);
        }
        String weight = OtsApplicationAdapterSdkConstants.req_v2_interface_weight();
        List list = new ArrayList();//默认权重0
        switch (weight) {
            case "1":
                list = Lists.newArrayList(1);
                break;
            case "2":
                list = Lists.newArrayList(1,6);
                break;
            case "3":
                list = Lists.newArrayList(1,4,7);
                break;
            case "4":
                list = Lists.newArrayList(1,3,5,7);
                break;
            case "5":
                list = Lists.newArrayList(1,3,5,7,9);
                break;
            case "6":
                list = Lists.newArrayList(1,2,3,5,7,9);
                break;
            case "7":
                list = Lists.newArrayList(1,2,3,4,5,7,9);
                break;
            case "8":
                list = Lists.newArrayList(1,2,3,4,5,6,7,9);
                break;
            case "9":
                list = Lists.newArrayList(1,2,3,4,5,6,7,8,9);
                break;
            case "10":
                list = Lists.newArrayList(0,1,2,3,4,5,6,7,8,9);
                break;
        }
        log.info("interfaceDistributeStrategyImpl_weightDistribution_i={},weight={}",i,weight);
        String result = "1";
        if(list.contains(i)){
            result = "2";
        }
        return Result.success(result);
    }


}
